home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Gamma Fade 1.2 / C / gamma.c < prev    next >
Text File  |  1995-09-24  |  7KB  |  219 lines

  1. // File "gamma.c" - Source for Altering the Gamma Tables of GDevices
  2. //   Last updated 9/24/95, MJS
  3.  
  4. // * ****************************************************************************** *
  5. //
  6. //    This is the Source Code for the Gamma Utils Library file. Use this to build
  7. //        new functionality into the library or make an A4-based library. 
  8. //    See the header file "gamma.h" for much more information. -- MJS
  9. //
  10. // * ****************************************************************************** *
  11.  
  12. #include <GestaltEqu.h>
  13. #include <Quickdraw.h>
  14. #include <Traps.h>
  15. #include <Video.h>
  16. #include "gamma.h"
  17.  
  18. long            gammaUtilsInstalled;
  19. GammaTrackerHdl    gammaTables;
  20.  
  21. // * ****************************************************************************** *
  22. // * ****************************************************************************** *
  23.  
  24. pascal Boolean IsGammaAvailable() {
  25.     GDHandle theGDevice;
  26.  
  27.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  28.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(FALSE);
  29.     
  30.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice)) {
  31.         if (TestDeviceAttribute(theGDevice, screenDevice) && 
  32.                 TestDeviceAttribute(theGDevice, noDriver)) return(FALSE);
  33.         if ((*theGDevice)->gdType == fixedType) return(FALSE);
  34.         }
  35.         
  36.     return(TRUE);
  37.     }
  38.  
  39. // * ****************************************************************************** *
  40. // * ****************************************************************************** *
  41.  
  42. pascal Boolean IsOneGammaAvailable(GDHandle theGDevice) {
  43.     
  44.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  45.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(FALSE);
  46.     
  47.     if (TestDeviceAttribute(theGDevice, screenDevice) && 
  48.             TestDeviceAttribute(theGDevice, noDriver)) return(FALSE);
  49.     
  50.     if ((*theGDevice)->gdType == fixedType) return(FALSE);
  51.     
  52.     return(TRUE);
  53.     }
  54.  
  55. // * ****************************************************************************** *
  56. // * ****************************************************************************** *
  57.  
  58. pascal OSErr SetupGammaTools() {
  59.     short err=0;
  60.     GammaTrackerHdl tempHdl;
  61.     GammaTblPtr    masterGTable;
  62.     GDHandle theGDevice;
  63.  
  64.     if (gammaUtilsInstalled == kGammaUtilsSig) return(-1);
  65.     
  66.     gammaTables = 0;
  67.     gammaUtilsInstalled = kGammaUtilsSig;
  68.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice)) {
  69.         if (err = GetDevGammaTable(theGDevice, &masterGTable)) return(err);
  70.         
  71.         tempHdl = (GammaTrackerHdl) NewHandle(sizeof(GammaTracker));
  72.         if (tempHdl == 0) return(err = MemError());
  73.         
  74.         (*tempHdl)->size = sizeof(GammaTbl) + masterGTable->gFormulaSize +
  75.                 (masterGTable->gChanCnt * masterGTable->gDataCnt * masterGTable->gDataWidth / 8);
  76.         (*tempHdl)->dataOffset = masterGTable->gFormulaSize;
  77.         (*tempHdl)->theGDevice = theGDevice;
  78.         
  79.         (*tempHdl)->saved = (GammaTblHandle) NewHandle((*tempHdl)->size);
  80.         if ((*tempHdl)->saved == 0) return(err = MemError());
  81.         (*tempHdl)->hacked = (GammaTblHandle) NewHandle((*tempHdl)->size);
  82.         if ((*tempHdl)->hacked == 0) return(err = MemError());
  83.     
  84.         BlockMove((Ptr) masterGTable, (Ptr) *(*tempHdl)->saved, (*tempHdl)->size);
  85.         
  86.         (*tempHdl)->next = gammaTables;
  87.         gammaTables = tempHdl;
  88.         }
  89.  
  90.     return(0);
  91.     }
  92.  
  93. // * ****************************************************************************** *
  94. // * ****************************************************************************** *
  95.  
  96. pascal OSErr DoGammaFade(short percent) {
  97.     short err=0;
  98.     register long size, i, theNum;
  99.     GammaTrackerHdl tempHdl;
  100.     unsigned char *dataPtr;
  101.  
  102.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  103.     
  104.     for(tempHdl = gammaTables; tempHdl; tempHdl = (*tempHdl)->next) {
  105.     
  106.         BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  107.         dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  108.         size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  109.         
  110.         for(i=0; i < size; i++) {
  111.             theNum = dataPtr[i];
  112.             theNum = (theNum * percent) / 100;
  113.             dataPtr[i] = theNum;
  114.             }
  115.         
  116.         if (err = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked))
  117.             return(err);
  118.         }
  119.         
  120.     return(0);
  121.     }
  122.  
  123. // * ****************************************************************************** *
  124. // * ****************************************************************************** *
  125.  
  126. pascal OSErr DoOneGammaFade(GDHandle theGDevice, short percent) {
  127.     short err=0;
  128.     register long size, i, theNum;
  129.     GammaTrackerHdl tempHdl;
  130.     unsigned char *dataPtr;
  131.  
  132.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  133.     for(tempHdl = gammaTables; tempHdl && (theGDevice != (*tempHdl)->theGDevice);
  134.             tempHdl = (*tempHdl)->next);
  135.     if (! tempHdl) return(err = -1);
  136.  
  137.     BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  138.     dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  139.     size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  140.     
  141.     for(i=0; i < size; i++) {
  142.         theNum = dataPtr[i];
  143.         theNum = (theNum * percent) / 100;
  144.         dataPtr[i] = theNum;
  145.         }
  146.     
  147.     err = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked);
  148.     
  149.     return(err);
  150.     }
  151.  
  152. // * ****************************************************************************** *
  153. // * ****************************************************************************** *
  154.  
  155. pascal OSErr DisposeGammaTools() {
  156.     GammaTrackerHdl tempHdl, nextHdl;
  157.  
  158.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  159.     for(tempHdl = gammaTables; tempHdl; tempHdl = nextHdl) {
  160.         nextHdl = (*tempHdl)->next;
  161.         DisposeHandle((Handle) (*tempHdl)->saved);
  162.         DisposeHandle((Handle) (*tempHdl)->hacked);
  163.         DisposeHandle((Handle) tempHdl);
  164.         }
  165.         
  166.     gammaUtilsInstalled = 0;
  167.     return(0);
  168.     }
  169.  
  170. // * ****************************************************************************** *
  171. // * ****************************************************************************** *
  172.  
  173. pascal OSErr GetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  174.     short err=0;
  175.     CntrlParam  *myCPB;
  176.  
  177.     ((long *) theTable)[0] = 0;
  178.  
  179.     if (IsOneGammaAvailable(theGDevice) == 0) return(-1);
  180.             
  181.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  182.     myCPB->csCode = cscGetGamma;
  183.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  184.     ((GammaTblPtr **) myCPB->csParam)[0] = theTable;
  185.     err = PBStatus((ParmBlkPtr) myCPB, 0);
  186.  
  187.     DisposePtr((Ptr) myCPB);
  188.     return(err);
  189.     }
  190.  
  191. // * ****************************************************************************** *
  192. // * ****************************************************************************** *
  193.  
  194. pascal OSErr SetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  195.     CntrlParam *myCPB;
  196.     short err=0;
  197.     CTabHandle cTab;
  198.     GDHandle saveGDevice;
  199.  
  200.     if (IsOneGammaAvailable(theGDevice) == 0) return(-1);
  201.  
  202.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  203.     myCPB->csCode = cscSetGamma;
  204.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  205.     ((GammaTblPtr **) myCPB->csParam)[0] = theTable;
  206.     err = PBControl((ParmBlkPtr) myCPB, 0);
  207.  
  208.     if (err == 0) {
  209.         saveGDevice = GetGDevice();
  210.         SetGDevice(theGDevice);
  211.          cTab = (*(*theGDevice)->gdPMap)->pmTable;
  212.         SetEntries (0, (*cTab)->ctSize, (*cTab)->ctTable);
  213.         SetGDevice(saveGDevice);
  214.         }
  215.  
  216.     DisposePtr((Ptr) myCPB);
  217.     return (err);
  218.     }
  219.